<?php
/**
 * 店铺模型
 * @version 1.0
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Shop\Model;

use Think\Model;

class StoreModel extends Model{
    
    protected $tableName = 'store';
    
    /**
     * 自动验证
     * @var unknown
     */
    protected $_validate = array(
            array('logo','require','logo必须上传！')
    );

    
    /**
     * 获取店铺信息
     * @param unknown $map
     * @param string $field
     */
    public function getInfo($map=array(),$field=true){
        if(empty($map)){
            return false;
        }
        $data = $this->where($map)->field($field)->find();
        return $data;
    }
    
    
    /**
     * 入驻详细信息
     * @param int $member_id
     */
    public function info($member_id){
        $map['member_id'] = $member_id;
        $map['status'] = array('gt',0);
        $field = 'ctime,atime';
        $data = M('store_apply')->where($map)->field($field,true)->find();
        //店铺经营类目
        $sell_class_name = unserialize($data['store_class_names']);
        $data['class_rate'] = explode(',',$data['store_class_rates']);
        foreach ($sell_class_name as $key=>$val){
            $sell_class_name[$key] = explode(',',trim($val,','));
        }
        $data['sell_cate'] = $sell_class_name;
        //查询店铺等级
        $data['grade_name'] = M('StoreGrade')->where(array('id='.$data['grade_id']))->getField('name');
        $data['rate'] = M('Store')->where('member_id='.$member_id)->getField('rate');
        return $data;
    }
    
    
    /**
     * 店铺内商品分类
     */
    public function storeClass($store_id=0,$field=true){
        $model = M('StoreClass');
        $map  = array('status' => array('gt', -1));
        $map  = array('store_id' => array('eq', $store_id));
        $list = $model->field($field)->where($map)->select();
        $list = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_', $root = 0);
        return $list;
    }
    
    
}
